/**
 * File:		RequestInterpreter.java
 * Project:		Virtual Advisor
 * Module:		Data Management
 * Author:		Rotem Ganel
 * Created:		May 10, 2011
 * Updated:		May 10, 2011
 * Version:		1 (in progress)
 */
package clientSocket;

import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;

import clientApp.Course;
import clientApp.Degree;
import clientApp.User;
import clientSocket.Request.TYPE;

//TODO: implement setFilenames on server side, get rid of that in VA

public class RequestInterpreter {
	public static void commence() throws UnknownHostException, IOException {
		ClientPortal.instantiate();
	}
	
	public static ArrayList<Course> retrieveCatalog() throws IOException, ClassNotFoundException {
		try	{
//			System.out.println("Retrieve catalog");
			ClientPortal.sendObject(new Request(TYPE.retrieveCatalog));
			return (ArrayList<Course>)ClientPortal.receiveData();
		} catch (SocketException e) {
			System.out.println("Unable to connect to server.");
			terminate();
			System.exit(-1);
			return null;
		}
	}
	
	public static ArrayList<Degree> getDegrees() throws IOException, ClassNotFoundException {
		try {
//			System.out.println("Get degrees");
			ClientPortal.sendObject(new Request(TYPE.getDegrees));
			return (ArrayList<Degree>)ClientPortal.receiveData();
		} catch (SocketException e) {
			System.out.println("Unable to connect to server.");
			terminate();
			System.exit(-1);
			return null;
		}
	}
	
	public static ArrayList<User> getAllUsers() throws IOException, ClassNotFoundException {
		try {
//			System.out.println("Get users");
			ClientPortal.sendObject(new Request(TYPE.getUsers));
			return (ArrayList<User>)ClientPortal.receiveData();
		} catch (SocketException e) {
			System.out.println("Unable to connect to server.");
			terminate();
			System.exit(-1);
			return null;
		}
	}
	
	public static int getUserType(String username) throws IOException, ClassNotFoundException {
		try {
//			System.out.println("Get user type");
			ClientPortal.sendObject(new Request(TYPE.getUserType, new String[]{username}));
			return (Integer)ClientPortal.receiveData();
		} catch (SocketException e) {
			System.out.println("Unable to connect to server.");
			terminate();
			System.exit(-1);
			return -1;
		}
	}
	
	public static User findUser(String username, String password) throws IOException, ClassNotFoundException {
		try {
//			System.out.println("Find User");
			ClientPortal.sendObject(new Request(TYPE.findUser, new String[]{username, password}));
			return (User)ClientPortal.receiveData();
		} catch (SocketException e) {
			System.out.println("Unable to connect to server.");
			terminate();
			System.exit(-1);
			return null;
		}
	}
	
	public static void saveUsers(ArrayList<User> users) throws IOException, ClassNotFoundException {
//			System.out.println("Save users");
			try {
				ClientPortal.sendObject(new Request(TYPE.saveUsers, users));
				ClientPortal.receiveData();
			} catch (IOException e) {
				System.out.println("Unable to connect to server.");
				terminate();
				System.exit(-1);
			}
	}
	
	public static void saveCatalog(ArrayList<Course> catalog) throws IOException, ClassNotFoundException {
//		System.out.println("Save catalog");
		try {
			ClientPortal.sendObject(new Request(TYPE.saveCatalog, catalog));
			ClientPortal.receiveData();
		} catch (IOException e) {
			System.out.println("Unable to connect to server.");
			terminate();
			System.exit(-1);
		}
	}
	
	public static void saveCurriculum(ArrayList<Degree> curriculum) throws IOException, ClassNotFoundException {
//		System.out.println("Save curriculum");
		try {
			ClientPortal.sendObject(new Request(TYPE.saveCurriculum, curriculum));
			ClientPortal.receiveData();
		} catch (IOException e) {
			System.out.println("Unable to connect to server.");
			terminate();
			System.exit(-1);
		}
	}
	
	public static void terminate() throws IOException {
		ClientPortal.terminate();
	}
}
